/**
 * 文件下载
 * @param {object} [opts = {}] - [相应的参数配置]
 * @param {string} [opts.type = 'xlsx'] - [文件后缀,默认为excel]
 * @param {string} [opts.url = ''] - [请求接口地址]
 * @param {string} [opts.filename = '导出文件'] - [文件名称]
 * @param {string} [opts.method = 'post'] - [请求方式，默认为post]
 * @param {object} [opts.data = {}] - [请求需要携带过去的参数]
 * @param {function} [opts.error = funciton(){}] - [报错时的回调]
 * @param {function} [opts.success = function(){}] - [成功时的回调]
 * @throw {error} 请传入文件接口地址
 */
/**
 * 文件下载
 * @param {object} [opts = {}] - [相应的参数配置]
 * @param {string} [opts.type = 'xlsx'] - [文件后缀,默认为excel]
 * @param {string} [opts.url = ''] - [请求接口地址]
 * @param {string} [opts.filename = '导出文件'] - [文件名称]
 * @param {string} [opts.method = 'post'] - [请求方式，默认为post]
 * @param {object} [opts.data = {}] - [请求需要携带过去的参数]
 * @param {function} [opts.error = funciton(){}] - [报错时的回调]
 * @param {function} [opts.success = function(){}] - [成功时的回调]
 * @throw {error} 请传入文件接口地址
 */
function downloadFile (opts = {}, axios) {
    console.log(opts)
    let options = Object.assign({}, {
        type: 'xls',
        url: '',
        filename: '导出文件',
        method: 'post',
        data: {},
        error: () => {
        },
        success: () => {
        }
    }, opts)
    if (!options.url) throw new Error('请传入文件接口地址')
    axios({
        method: options.method,
        url: options.url,
        data: options.data,
        responseType: 'blob'
    }).then((res) => {
        console.log(res)
        let name = res.headers['content-disposition'].split('filename=')[1]
        if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveOrOpenBlob(res.data, name)
        }
        let url = window.URL.createObjectURL(res.data)
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', decodeURI(name))
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        link = null
        options.success()
    }).catch((err) => {
        options.error(err)
    })
}
export {
    downloadFile
}